from gevent import monkey
from gevent.pywsgi import WSGIServer

from flask import Flask
from flask import request
from flask import redirect
from flask import jsonify
from flask_sqlalchemy import SQLAlchemy

from app.comm.db import db
from app.system import auth
from app.system.log import logservice
from app.system.user import userservice 
from app.system.role import roleservice
from app.system.dept import deptservice
from app.system.menu import menuservice
from app.system.post import postservice
from app.system.dict import dictservice
from app.system.config import configservice
from app.system.notice import noticeservice
from app.datares.appmenu import appmenuservice
from app.comm.baidu import baiduservice

#monkey.patch_all()

#导入Flask
app = Flask(__name__ ,static_url_path='')

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:dataxiu~1@dataxiu.com:3306/dataxiu?charset=utf8'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_POOL_SIZE'] = 100

db = SQLAlchemy(app)


@app.before_request
def before_request():   
    #baidugeocode("宋家坪")
    if request.path[0:5]=="/rest" :
        print("授权请求地址：",request.path)  
        #auth.identify(request)

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    if request.method == 'OPTIONS':
        response.headers['Access-Control-Allow-Methods'] = 'DELETE, GET, POST, PUT'
        headers = request.headers.get('Access-Control-Request-Headers')
        if headers:
            response.headers['Access-Control-Allow-Headers'] = headers
    return response


#创建一个Flask实例
app.register_blueprint(logservice)
app.register_blueprint(userservice)
app.register_blueprint(roleservice)
app.register_blueprint(deptservice)
app.register_blueprint(menuservice) 
app.register_blueprint(postservice)  
app.register_blueprint(dictservice)  
app.register_blueprint(configservice)  
app.register_blueprint(noticeservice)  
app.register_blueprint(appmenuservice)
app.register_blueprint(baiduservice)  

#设置路由，即url
@app.route('/')
#url对应的函数
def hello_world():
    #返回的页面
    
    return 'Hello World!'


#这个不是作为模块导入的时候运行，比如这个文件为aa.py，当python aa.py就执行这个代码。如果是在其他文件import的话，不执行这个文件。（这个属于python的基础知识）
if __name__ == '__main__':
    #app.run(debug=True) #
    http_server = WSGIServer(('0.0.0.0', 5000), app)
    http_server.serve_forever()